﻿using MultiAgentsClient.ChatDataStructures;
using MultiAgentsServer.Scripts.Services;
using MultiAgentsShared;

namespace MultiAgentsServer.Scripts.ToolSets;

[GptToolSet("Workspace Operation Tools Provider", "")]
public class WorkspaceOperationToolsProvider
{
    private static string WorkspaceName = "";
    private static string WorkspacePathRoot = "";
    private static string WorkspacePath = "";

    public WorkspaceOperationToolsProvider(string workspaceName, string workspacePathRoot)
    {
        WorkspaceName = workspaceName;
        WorkspacePathRoot = workspacePathRoot;
        var wsn = ChatStringExtension.NameAsSystemReadable(workspaceName);
        var pathForProject = $"{wsn}_{ChatStringExtension.NameAsSystemReadable(DateTime.Now.ToString())}";
        WorkspacePath = WorkspacePathRoot + $"\\{pathForProject}\\";
    }

    [GptFunc("Given relative file path, check if a file exists within gpt-workspace", false)]
    public static string CheckFileExistInWorkspace(
        [GptFuncParam("Relative filepath to the file within gpt-workspace")] string relativeFilePath
        )
    {
        try
        {
            return FileOperationService.CheckFileExist(WorkspacePath + relativeFilePath).ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    [GptFunc("Given relative file path, load the file contents within gpt-workspace", false)]
    public static string LoadFileFromWorkspace(
        [GptFuncParam("Relative filepath to the file within gpt-workspace")] string relativeFilePath
        )
    {
        try
        {
            return FileOperationService.LoadFile(WorkspacePath + relativeFilePath);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    [GptFunc("Given relative filepath, create the file within gpt-workspace and fill in fileContents.", false)]
    public static string CreateFileInWorkspace(
        [GptFuncParam("relative filepath to the file within gpt-workspace")] string relativeFilePath,
        [GptFuncParam("the file content to be fill in the file")] string fileContent
        )
    {
        try
        {
            return FileOperationService.CreateFile(WorkspacePath + relativeFilePath, fileContent);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    [GptFunc("Explore the files/folders within gpt-workspace", false)]
    public static string ExploreWorkspaceDirectory()
    {
        try
        {
            return FileOperationService.ExploreDirectory(WorkspacePath);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
}
